﻿Imports System

Namespace Global.MS.Internal.Threading

    Public Delegate Sub TickEventHandler(ByVal sender As Object, ByVal e As EventArgs(Of TimeSpan))

    Public NotInheritable Class Win32Timer
        Implements IDisposable

        Public Event Tick As TickEventHandler

        Public Property Interval() As Integer
        Public Property Hwnd() As Integer
        Public Property EventID() As Integer

        Public ReadOnly Property Invalid() As Boolean
            Get
                Return Me.m_Invalid
            End Get
        End Property

        Private m_Invalid As Boolean = False

        Public Sub New(ByVal hwnd As Integer, ByVal nIdEvent As Integer, ByVal interval As Integer, Optional ByVal callback As TickEventHandler = Nothing)
            Me.Interval = interval
            Me.EventID = nIdEvent
            Me.Hwnd = hwnd
            If callback IsNot Nothing Then AddHandler Me.Tick, callback
        End Sub

        Public Sub FireTick(ByVal t As TimeSpan)
            RaiseEvent Tick(Me, New EventArgs(Of TimeSpan)(t))
        End Sub

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                    Me.m_Invalid = True
                End If
                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

End Namespace